import { createRouter, createWebHistory } from 'vue-router'
import UserLayout from '@/views/user/UserLayout.vue'
import AdminLayout from '@/views/admin/AdminLayout.vue'
import { useAdminStore } from '@/stores/admin'
import { useUserStore } from '@/stores/user'
import { setupAdminPermissionGuard } from './permission'

// 用户端路由
const userRoutes = [
  {
    path: '/user/login',
    name: 'UserLogin',
    component: () => import('@/views/user/login/UserLogin.vue'),
    meta: {
      title: '用户登录'
    }
  },
  {
    path: '/user',
    component: UserLayout,
    meta: {
      title: '用户端'
    },
    children: [
      {
        path: '',
        redirect: '/user/home'
      },
      {
        path: 'home',
        name: 'BookSharingHome',
        component: () => import('@/views/user/book-sharing-home/BookSharingHome.vue'),
        meta: {
          title: '用户端-首页'
        }
      },
      {
        path: 'my-posts',
        name: 'MyBookPost',
        component: () => import('@/views/user/my-post/MyBookPost.vue'),
        meta: {
          title: '用户端-我的发布'
        }
      },
      {
        path: 'drafts',
        name: 'Drafts',
        component: () => import('@/views/user/drafts/Drafts.vue'),
        meta: { 
          title: '用户端-草稿箱' 
        }
      },
      {
        path: 'my-likes',
        name: 'MyLikes',
        component: () => import('@/views/user/my-like/MyLikes.vue'),
        meta: {
          title: '用户端-我的点赞'
        }
      },
      {
        path: 'notifications',
        name: 'NotificationCenter',
        component: () => import('@/views/user/notification/NotificationCenter.vue'),
        meta: {
          title: '用户端-通知中心'
        }
      },
      {
        path: 'profile',
        name: 'UserProfile',
        component: () => import('@/views/user/user-profile/UserProfile.vue'),
        meta: {
          title: '用户端-个人中心'
        }
      }
    ]
  },
  {
    path: '/user/post',
    name: 'PostBook',
    component: () => import('@/views/user/post/PostBook.vue'),
    meta: {
      title: '分享书籍',
      target: '_blank'
    }
  },
  {
    path: '/user/drafts/edit/:id',
    name: 'DraftEdit',
    component: () => import('@/views/user/drafts/DraftEdit.vue'),
    meta: {
      title: '草稿编辑',
      target: '_blank'
    }
  },
  {
    path: '/books/:id',
    name: 'BookDetail',
    component: () => import('@/views/user/book-sharing-home/book-detail/BookDetail.vue'),
    meta: {
      title: '详情',
      target: '_blank'
    }
  },
  {
    path: '/my-post/edit/:id',
    name: 'EditBookPost',
    component: () => import('@/views/user/my-post/EditBookPost.vue'),
    meta: {
      requiresAuth: true,
      title: '编辑书籍',
      target: '_blank'
    }
  }
]

// 管理端路由
const adminRoutes = [
  {
    path: '/admin/login',
    name: 'AdminLogin',
    component: () => import('@/views/admin/login/AdminLogin.vue'),
    meta: {
      title: '管理员登录'
    }
  },
  {
    path: '/admin',
    component: AdminLayout,
    meta: {
      title: '管理端'
    },
    children: [
      {
        path: '',
        redirect: '/admin/data-display'
      },
      {
        path: 'data-display',
        name: 'DataDisplay',
        component: () => import('@/views/admin/data-display/DataDisplay.vue'),
        meta: {
          title: '管理端-数据看板'
        }
      },
      {
        path: 'book-manage',
        name: 'BookManage',
        component: () => import('@/views/admin/book-manage/BookManage.vue'),
        meta: {
          title: '管理端-书籍管理'
        }
      },
      {
        path: 'report-manage',
        name: 'ReportManage',
        component: () => import('@/views/admin/report-manage/ReportManage.vue'),
        meta: {
          title: '管理端-举报管理'
        }
      },
      {
        path: 'user-manage',
        name: 'MemberManage',
        component: () => import('@/views/admin/member-manage/UserManage.vue'),
        meta: {
          title: '管理端-用户管理'
        }
      },
      {
        path: 'admin-manage',
        name: 'AdminManage',
        component: () => import('@/views/admin/member-manage/AdminManage.vue'),
        meta: {
          title: '管理端-管理员管理'
        }
      },
      {
        path: 'admin-profile',
        component: () => import('@/views/admin/admin-profile/AdminInfo.vue'),
        meta: {
          title: '管理端-个人信息',
        }
      },
      {
        path: 'admin-password',
        component: () => import('@/views/admin/admin-profile/AdminPassword.vue'),
        meta: {
          title: '管理端-修改密码',
        }
      },
      {
        path: 'no-permission',
        component: () => import('@/views/error/NoPermission.vue'),
        meta: { requiresAuth: true }
      }
    ]
  }
]

const router = createRouter({
  history: createWebHistory(),
  routes: [
    {
      path: '/',
      redirect: '/user/home'
    },
    ...userRoutes,
    ...adminRoutes
  ]
})

// 路由守卫
router.beforeEach((to, from, next) => {
  const adminStore = useAdminStore()
  const userStore = useUserStore()

  if (to.path.startsWith('/admin') && to.path !== '/admin/login') {
    if (!adminStore.isLoggedIn) {
      next('/admin/login')
      return
    }
  }

  if (to.path.startsWith('/user') && 
      to.path !== '/user/login' && 
      to.path !== '/user/home') {
    if (!userStore.isLoggedIn) {
      next('/user/login')
      return
    }
  }

  // 设置标题
  if (to.meta.title) {
    document.title = to.meta.title
  }

  next()
})

// 设置权限守卫
setupAdminPermissionGuard(router)

export default router 